package com.baiqian.doupo.douji;

import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.text.TranslationTextComponent;
import org.apache.logging.log4j.LogManager;

public abstract class DouJi {
    public DouJi() {
        douji_icon=0;points=0;
    }
    public DouJi(String douji_name,int douji_icon,int douji_level_big,int douji_level_middle,int douji_keep_time,
                 int douji_use_rebirth_time,int douji_property,double douji_attack_num,double douji_defense_num,
                 double douji_recover_num,int douji_use_num,int douji_type,boolean is_have_entiy_modle,boolean is_ex_to_pay){
        this.douji_name=douji_name;
        this.douji_icon=douji_icon;
        this.douji_level_big=douji_level_big;
        this.douji_level_middle=douji_level_middle;
        this.douji_keep_time=douji_keep_time;
        this.douji_use_rebirth_time=douji_use_rebirth_time;
        this.douji_property=douji_property;
        this.douji_attack_num=douji_attack_num;
        this.douji_defense_num=douji_defense_num;
        this.douji_recover_num=douji_recover_num;
        this.douji_use_num=douji_use_num;
        this.douji_type=douji_type;
        this.is_have_entiy_modle=is_have_entiy_modle;
        points=douji_use_num;
        rebirth_time=0;
        use_time=0;
        this.is_ex_to_pay=is_ex_to_pay;
        douqi_be_use=0;
    }

    public static DouJi getDouJi(String douji_name){
        switch (douji_name) {
            case "狮山裂":
                return new DouJi_ShiShanLie();
            case "吸掌":
                return new DouJi_XiZhang();
            case "火鼎":
                return new DouJi_HuoDing();
            case "灵魂感知":
                return new DouJi_LingHunGanZhi();
            case "斗气输送":
                return new DouJi_DouQiShuSong();
        }
        return null;
    }

    /**
     * 斗技名
     */
    public String douji_name;
    /**
     * 斗技图标id
     */
    public int douji_icon;//
    /**
     * 斗技等级：天地玄黄
     */
    public int douji_level_big;//
    /**
     * 斗技等级：高中初
     */
    public int douji_level_middle;//
    /**
     * 斗技持续消耗时间
     */
    public int douji_keep_time;//
    /**
     * 斗技属性
     */
    public int douji_property;//
    /**
     * 使得每1点斗气多出多少攻击
     */
    public double douji_attack_num;//
    /**
     * 使得每1点斗气多出多少防御
     */
    public double douji_defense_num;//
    /**
     * 使得每1点斗气多出多少治愈
     */
    public double douji_recover_num;//
    /**
     * 斗技可使用最大次数
     */
    public int douji_use_num;//
    /**
     * 斗技次数恢复1所需要的冷却时间
     */
    public int douji_use_rebirth_time;//
    /**
     * 斗技类型：1抛出型；2激光型；3附加攻击型，4附加防御型，5领域型
     */
    public int douji_type;
    /**
     * 是否产出模型实体
     */
    public boolean is_have_entiy_modle;
    /**
     * 当前可以使用次数
     */
    public int points;
    /**
     * 剩余的冷却时间
     */
    public int rebirth_time;
    /**
     * 剩余的持续时间
     */
    public int use_time;
    /**
     * 额外的斗气应该被消耗吗
     */
    public boolean is_ex_to_pay;
    /**
     * 被用在该斗技上的斗气量(未压缩)
     */
    public double douqi_be_use;

    /**
     * 获取该斗技的基本斗气消耗
     */
    final  private double getDouQiNeedFromDouJi(){
        //斗技的作用是对百分比输出的斗气的攻击力进行百分比的增幅
//               douji_level_big;//斗技等级：天地玄黄
//               douji_level_middle;//斗技等级：高中初
        double douqi_base_pay=Double.MAX_VALUE;//该斗技等级下的斗气量基本消耗
        switch (douji_level_big){
            case 1:
                switch (douji_level_middle){
                    case 1:douqi_base_pay=460;break;//1星斗宗
                    case 2:douqi_base_pay=143;break;//6星斗皇
                    case 3:douqi_base_pay=103;break;//1星斗皇
                }
            case 2:
                switch (douji_level_middle){
                    case 1:douqi_base_pay=23.2079;break;//1星斗王
                    case 2:douqi_base_pay=7.21;break;//6星斗灵
                    case 3:douqi_base_pay=5.21095;break;//一星斗灵
                }
            case 3:
                switch (douji_level_middle){
                    case 1:douqi_base_pay=1.17;break;//1星大斗师
                    case 2:douqi_base_pay=0.3635;break;//6星斗师
                    case 3:douqi_base_pay=0.2627;break;//1星斗师可以使用10s
                }
            case 4:
                switch (douji_level_middle){
                    case 1:douqi_base_pay=0.05895;break;//一星斗者可以使用10s
                    case 2:douqi_base_pay=0.0183;break;//六段斗之气可以使用10s
                    case 3:douqi_base_pay=0.0132;break;//一段斗之气可以使用10s
                }
        }
        if(douji_icon==4||douji_icon==6){//如果是灵魂感知或者灵魂注入则不消耗斗气
            douqi_base_pay=0;
        }else if(douji_icon==3){//召唤火鼎的每tick消耗
            douqi_base_pay=0.0132;
        }else if(douji_icon==5){//斗气输送的每tick消耗
            douqi_base_pay=0.05895;
        }
        return douqi_base_pay;
    }

    /**
     * 获取该斗技的基本灵魂力消耗
     */
    final private double getSoulNeedFromDouJi(){
        if(douji_icon==4)
            return 0.0005;
        if(douji_icon==6)
            return 0.001;
        return 0;
    }

    /**
     * 开始冷却
     */
    final public boolean startRebirth(){
        if(points<douji_use_num) //点数不足才能冷却
            if(use_time==0)//没在使用才能冷却
                if(rebirth_time==0) {//没在冷却才能冷却
                    rebirth_time = douji_use_rebirth_time;
                    LogManager.getLogger().info("开始冷却");
                    return true;
                }
        return false;
    }

    /**
     * 是否正在冷却
     */
    final public boolean isRebirthing(){
        if(rebirth_time==0)
            return false;
        --rebirth_time;
        if(rebirth_time==0) {
            ++points;
            if(points<douji_use_num) {
                LogManager.getLogger().info("一次冷却完毕");
                startRebirth();//开始下一轮冷却
            }
        }
        return true;
    }

    /**
     * 获取玩家支出的斗气
     * */
    final private double getDouqiPayFromPlayer(IDouQiCapability l){
        //获取压缩后的斗气
        double douqi_have = l.getDouQi(1);
        double douqi_pay = l.getDouQiHaveMax()*l.getExDouQiReleasePercentage();//获取支付的斗气
        if(douqi_pay>douqi_have)
            douqi_pay=douqi_have;
        return douqi_pay;
    }

    /**
     * 获取玩家支出的灵魂
     * */
    final private double getSoulPayFromPlayer(IDouQiCapability l){
        if(l.getSoul()>getSoulNeedFromDouJi())
            return getSoulNeedFromDouJi();
        else
            return l.getSoul();
    }


    /**
     * 是否有足够的斗气和灵魂支付此次消耗
     */
    final private boolean isEnoughPay(IDouQiCapability l){
        if(getDouqiPayFromPlayer(l)>=getDouQiNeedFromDouJi()&&getSoulPayFromPlayer(l)>=getSoulNeedFromDouJi())
            return true;
        return false;
    }

    /**
     * 开始使用
     */
    final public boolean start_use_time(IDouQiCapability l,PlayerEntity playerEntity){
        LogManager.getLogger().info("拥有斗气:"+l.getDouQi(1));
        LogManager.getLogger().info("所需基本斗气:"+getDouQiNeedFromDouJi());
        LogManager.getLogger().info("给予斗技的斗气:"+getDouqiPayFromPlayer(l));
        if(is_ex_to_pay)
            LogManager.getLogger().info("有多少斗气产生了额外威力:"+(getDouqiPayFromPlayer(l)-getDouQiNeedFromDouJi()));
        LogManager.getLogger().info("所需灵魂力:"+getSoulNeedFromDouJi());
        LogManager.getLogger().info("拥有灵魂力:"+getSoulPayFromPlayer(l));
        if(isEnoughPay(l)) {
            if (points > 0) {//点数充足才能使用
                if (use_time == 0||use_time==-1) {//使用完或者开关型斗技才能使用
                    if (rebirth_time == 0) {//冷却完才能使用
                        if(use_time==-1) {//已经开启，则关闭
                            use_time = 0;
                            ++points;
                            playerEntity.sendMessage(new TranslationTextComponent("你关闭了" + douji_name), playerEntity.getUniqueID());
                        }
                        else {
                            use_time = douji_keep_time;
                            --points;
                            playerEntity.sendMessage(new TranslationTextComponent("你发动了" + douji_name), playerEntity.getUniqueID());
                        }
                        LogManager.getLogger().info("开始使用");
                        if(is_have_entiy_modle){
                            createEntity(l,playerEntity);
                        }
                        return true;
                    }
                    LogManager.getLogger().info("还在冷却" + rebirth_time);
                }
                LogManager.getLogger().info("还在使用" + use_time);
            }
            LogManager.getLogger().info("点数不足:" + points);
        }
        LogManager.getLogger().info("斗气不足");
        return false;
    }

    /**
     * 是否正在使用*/
    final public boolean is_use_time_ing(IDouQiCapability l){
        if(use_time==0)
            return false;
        if(douji_icon!=4&&douji_icon!=5){//开关类斗技不需要消耗使用时间
            --use_time;
        }
        if(use_time==0){//普通类使用时间结束
            LogManager.getLogger().info("一次使用完毕");
            end_use_time();
        }
        return true;
    }
    /**停止使用*/
    final public boolean stop_use_time(){//斗气不足或灵魂力不足，强行终止
        if(use_time>0||use_time==-1) {
            LogManager.getLogger().info("中断使用");
            end_use_time();
            return true;
        }
        return false;
    }
    /**
     * 结束使用
     * */
    final private void end_use_time(){
        use_time = 0;
        startRebirth();
    }

    /**
     * 消耗斗气和灵魂，若足够被消耗，则进行生命增减以及运行useEx函数来执行斗技独特效果
     * */
    final public void use(IDouQiCapability l, PlayerEntity entity){//斗技使用效果
        if(isEnoughPay(l)){
            //消耗斗气，且被环境等影响所以实际支出的消耗会少
            if(is_ex_to_pay) {
                douqi_be_use=getDouqiPayFromPlayer(l);
                l.addDouQi(-douqi_be_use, 1, true, entity);
                douqi_be_use*=l.getExCompressAll();//解压
            }
            else {
                douqi_be_use=getDouQiNeedFromDouJi();
                l.addDouQi(-douqi_be_use, 1, true, entity);
                douqi_be_use*=l.getExCompressAll();//解压
            }
            l.addSoul(-getSoulPayFromPlayer(l),true);
            //成功消耗斗气后开始效果结算
            float heal_max = entity.getMaxHealth();//得到当前最大生命值
            float heal = entity.getHealth();//得到当前生命值
            heal += douji_recover_num;//计算应得的治愈量
            if(heal>heal_max)//解决治愈溢出问题
                heal=heal_max;
            entity.setHealth(heal);//开始治愈
            useEx(l,entity);
        }else{
            stop_use_time();//结束技能并立即冷却
        }
    }


    /**
     * 斗技独特效果实现函数
     * */
    public abstract void useEx(IDouQiCapability l, PlayerEntity entity);

    /**
     * 斗技对指定实体施加效果
     * */
    public abstract void addEffect(LivingEntity entity);

    /**
     * 生成实体
     */
    abstract protected void createEntity(IDouQiCapability l, PlayerEntity playerEntity);
}
